package com.xiaoxin.experience.system.config;

import com.xiaoxin.experience.util.IgnoredSSLSocketFactory;
import okhttp3.OkHttpClient;
import org.springframework.stereotype.Component;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author xiaoxin
 */
@Component
public class HttpsClient extends AcHttpClient
{
    private final OkHttpClient okHttpClient =  new OkHttpClient.Builder()
            .connectTimeout(10, TimeUnit.SECONDS)
            .writeTimeout(10,TimeUnit.SECONDS)
            .readTimeout(10, TimeUnit.SECONDS)
            .retryOnConnectionFailure(false)
            .sslSocketFactory(IgnoredSSLSocketFactory.getSocketFactory(), IgnoredSSLSocketFactory.getX509TrustManager())
            .hostnameVerifier((hostname,session)->true)
            .build();

    @Override
    protected OkHttpClient getHttpClient()
    {
        return okHttpClient;
    }


    public byte[] downloadUtilSuccess(String url)
    {
        byte[] downloadContent = null;
        while (Objects.isNull(downloadContent))
        {
            downloadContent = download(url);
        }
        return downloadContent;
    }

}
